﻿$import( "SmitearWebGL.Core.ResourceItem" );

SmitearWebGL.Resources.Sampler = SmitearWebGL.Core.ResourceItem.extend(
{
	/******** constructor ********/

	init : function()
	{
		this._super();
		this._registClass( "SmitearWebGL.Resources.Sampler" );

		/******** public property ********/

		this.addressModeU = null;
		this.addressModeV = null;
		this.addressModeW = null;
		this.magFilter = null;
		this.minFilter = null;
	}
	,

	/******** public method ********/

	setAddressModeU : function( mode )
	{
		this.addressModeU = this._convertAddressMode( mode );
	}
	,
	setAddressModeV : function( mode )
	{
		this.addressModeV = this._convertAddressMode( mode );
	}
	,
	setAddressModeW : function( mode )
	{
		this.addressModeW = this._convertAddressMode( mode );
	}
	,
	setMagFilter : function( filter )
	{
		this.magFilter = this._convertMagFilter( filter );
	}
	,
	setMinFilter : function( filter )
	{
		this.minFilter = this._convertMinFilter( filter );
	}
	,

	/******** private method ********/

	_convertAddressMode : function( mode )
	{
		var gl_mode = this.context.gl.REPEAT;
		switch ( mode )
		{
			case SmitearWebGL.Resources.Sampler.WRAP :
				gl_mode = this.context.gl.REPEAT;
				break;
				
			case SmitearWebGL.Resources.Sampler.MIRROR :
				gl_mode = this.context.gl.MIRRORED_REPEAT;
				break;
				
			case SmitearWebGL.Resources.Sampler.CLAMP :
				gl_mode = this.context.gl.CLAMP_TO_EDGE;
				break;
				
			case SmitearWebGL.Resources.Sampler.BORDER :
				gl_mode = this.context.gl.CLAMP_TO_EDGE;
				break;
				
			default :
				$WARNING( "Unknown Address mode" );
				break;
		}
		return gl_mode;
	}
	,
	_convertMinFilter : function( filter )
	{
		switch ( filter )
		{
			case SmitearWebGL.Resources.Sampler.NONE :
			case SmitearWebGL.Resources.Sampler.POINT :
				return this.context.gl.NEAREST;
			case SmitearWebGL.Resources.Sampler.LINEAR :
			case SmitearWebGL.Resources.Sampler.ANISOTROPIC :
				return this.context.gl.LINEAR;
			case SmitearWebGL.Resources.Sampler.MIPMAP_POINT :
				return this.context.gl.LINEAR_MIPMAP_NEAREST;
			case SmitearWebGL.Resources.Sampler.MIPMAP_LINEAR :
				return this.context.gl.LINEAR_MIPMAP_LINEAR;
		}
		$WARNING( "Unknown filter." );
		return this.context.gl.LINEAR_MIPMAP_LINEAR;
	}
	,
	_convertMagFilter : function( filter )
	{
		switch ( filter )
		{
			case SmitearWebGL.Resources.Sampler.NONE :
			case SmitearWebGL.Resources.Sampler.POINT :
			return this.context.gl.NEAREST;
			case SmitearWebGL.Resources.Sampler.LINEAR :
			case SmitearWebGL.Resources.Sampler.ANISOTROPIC :
			return this.context.gl.LINEAR;
		}
		$WARNING( "Unknown filter." );
		return this.context.gl.LINEAR;
	}
	,

	/******** inherit method ********/
	
	_createResource : function()
	{
		this._super();
		
		this.addressModeU = this.context.gl.REPEAT;
		this.addressModeV = this.context.gl.REPEAT;
		this.addressModeW = this.context.gl.REPEAT;
		this.magFilter = this.context.gl.LINEAR;
		this.minFilter = this.context.gl.LINEAR_MIPMAP_LINEAR;
	}
	,
	_destroyResource : function()
	{
		this._super();

		this.addressModeU = null;
		this.addressModeV = null;
		this.addressModeW = null;
		this.magFilter = null;
		this.minFilter = null;
	}
}
);

/******** static property ********/

SmitearWebGL.Resources.Sampler.WRAP = 0;
SmitearWebGL.Resources.Sampler.MIRROR = 1;
SmitearWebGL.Resources.Sampler.CLAMP = 2;
SmitearWebGL.Resources.Sampler.BORDER = 3;

SmitearWebGL.Resources.Sampler.NONE = 0;
SmitearWebGL.Resources.Sampler.POINT = 1;
SmitearWebGL.Resources.Sampler.LINEAR = 2;
SmitearWebGL.Resources.Sampler.ANISOTROPIC = 3;
SmitearWebGL.Resources.Sampler.MIPMAP_POINT = 4;
SmitearWebGL.Resources.Sampler.MIPMAP_LINEAR = 5;